package webservice;

import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.xml.sax.SAXException;
import repositories.IPlaylistRepository;
import repositories.ITrackRepository;
import repositories.PlaylistRepository;
import repositories.TrackRepository;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Wrflohil
 * Date: 13-6-13
 * Time: 9:32
 * To change this template use File | Settings | File Templates.
 */
@Endpoint
public class PlaylistGenerator {
    public static final String NAMESPACE_URL = "http://www.han.nl/schemas/messages";

    private Marshaller marshaller;
    private Unmarshaller unmarshaller;
    private IPlaylistRepository playlistRepository;
    private ITrackRepository trackRepository;

    public PlaylistGenerator(Marshaller marshaller, Unmarshaller unmarshaller, ITrackRepository trackRepository, IPlaylistRepository playlistRepository) {
        this.marshaller = marshaller;
        this.unmarshaller = unmarshaller;
        this.trackRepository = trackRepository;
        this.playlistRepository = playlistRepository;
    }

    @SuppressWarnings({"unchecked", "deprecation"})
    @PayloadRoot(localPart = "GeneratePlaylistRequest", namespace = NAMESPACE_URL)
    public GeneratePlaylistResponse generatePlaylist(GeneratePlaylistRequest req) throws ParserConfigurationException, SAXException, IOException {
        String userName = req.getInputUser().getUserName();
        String searchValue = req.getInputTrack().getSearchValue();
        String playListName = req.getInputPlaylistname().getPlaylistname();

        List<Track> tracks = trackRepository.getTrackList(searchValue);
        Playlist playlist = playlistRepository.getPlaylist(userName, playListName, tracks);

        GeneratePlaylistResponse response = new GeneratePlaylistResponse();
        response.setResult(playlist);

        return response;
    }
}
